﻿using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Spotted.Controls
{
    public partial class SpottedImage : UserControl
    {
        public static readonly DependencyProperty ImageDataProperty =
            DependencyProperty.Register("ImageData", typeof(byte[]), typeof(SpottedImage),
                                        new PropertyMetadata(ImageDataChanged));

        public BitmapImage ImageSource
        {
            get { return (BitmapImage)TheImage.Source; }
            private set { TheImage.Source = value; }
        }

        public Stretch Stretch
        {
            get { return TheImage.Stretch; }
            set { TheImage.Stretch = value; }
        }

        public SpottedImage()
        {
            InitializeComponent();
        }

        private static void ImageDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = (SpottedImage) d;
            using (var memStream = new MemoryStream((byte[])e.NewValue))
            {
                var image = new BitmapImage();
                image.SetSource(memStream);

                control.ImageSource = image;
            }
        }
    }
}
